package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.activity.DeviceDetailActivity;
import com.audi.dto.DeviceItem;
import com.audi.utils.DeviceHelper;
import com.audi.view.DeviceUpdatePhoto;

public class ListDeviceCardAdapterInfoPlace extends BaseAdapter {
	private Activity mActivity;
	private DeviceUpdatePhoto mDeviceUpdatePhoto;
	private ArrayList<DeviceItem> mdeviceCardList;
	ImageView menudevicecard;
	private PopupMenu mPopMenu;
	SQLiteAsusNewHelper mSQLite;

	public ListDeviceCardAdapterInfoPlace(Activity activity,
			ArrayList<DeviceItem> devicecradList,
			DeviceUpdatePhoto mDeviceUpdatePhoto) {
		mActivity = activity;
		mdeviceCardList = devicecradList;
		this.mDeviceUpdatePhoto = mDeviceUpdatePhoto;
		mSQLite = new SQLiteAsusNewHelper(mActivity);
	}

	@Override
	public int getCount() {
		return mdeviceCardList.size();
	}

	@Override
	public DeviceItem getItem(int position) {
		
		return mdeviceCardList.get(position);
		
	}

	@Override
	public long getItemId(int position) {
		Log.d("log", "Checkstyle");
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup view) {
		DeviceItem item = mdeviceCardList.get(position);
		ViewHolder viewholder = null;
		
		// khoi tao cho viewholder
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_device_card_bot_new, null);

			viewholder.nameDevice = (TextView) convertView
					.findViewById(R.id.typeOfDeviceBot);
			viewholder.frameOnOff = (FrameLayout) convertView
					.findViewById(R.id.frame_onoff_bot);

			viewholder.txtOn = (TextView) convertView
					.findViewById(R.id.txt_off_bot);
			/*viewholder.txtTemp = (TextView) convertView
					.findViewById(R.id.txt_temp_bot);*/

			viewholder.txtOff = (TextView) convertView
					.findViewById(R.id.txt_on_bot);

			viewholder.imageMenu = (ImageView) convertView
					.findViewById(R.id.imv_device_tab_menu_Bot);

			viewholder.placeName = (TextView) convertView
					.findViewById(R.id.placeNameDeviceBot);

			viewholder.slidingDrawer = (SlidingDrawer) convertView
					.findViewById(R.id.slidingDrawer_bot);
			viewholder.imageWarning = (ImageView) convertView
					.findViewById(R.id.img_warning_bot);
			/*viewholder.imgTemp = (ImageView) convertView
					.findViewById(R.id.img_temp_bot);
			viewholder.layoutTemp = (FrameLayout) convertView
					.findViewById(R.id.layout_temp_bot);*/
			viewholder.txtOn.setVisibility(View.INVISIBLE);

			viewholder.txtOff.setVisibility(View.INVISIBLE);
			
			
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		// set data for listView

		int deviceType = DeviceHelper.getGroupType(item.getDeviceType());
		if (deviceType == 0) {
			//check style
			viewholder.txtOn.setVisibility(View.INVISIBLE);
			viewholder.txtOff.setVisibility(View.INVISIBLE);
			viewholder.frameOnOff.setVisibility(View.VISIBLE);
			viewholder.imageWarning.setVisibility(View.GONE);
			viewholder.layoutTemp.setVisibility(View.GONE);
			viewholder.slidingDrawer.setVisibility(View.VISIBLE);
			if (item.getDeviceStatus() == 1) {

				viewholder.slidingDrawer.open();
				viewholder.slidingDrawer
						.setBackgroundResource(R.drawable.icon_decive_on_small_source);

			} else {

				viewholder.slidingDrawer
						.setBackgroundResource(R.drawable.icon_device_off_small);

				viewholder.slidingDrawer.close();
			}
			;
		} else if (deviceType == 1) {
			viewholder.txtOn.setVisibility(View.INVISIBLE);
			viewholder.txtOff.setVisibility(View.INVISIBLE);
			viewholder.frameOnOff.setVisibility(View.GONE);
			viewholder.imageWarning.setVisibility(View.VISIBLE);
			viewholder.layoutTemp.setVisibility(View.GONE);
			if (item.getDeviceStatus() == 1) {

				viewholder.imageWarning
						.setImageResource(R.drawable.icon_warning_sensor_green);
			} else {
				viewholder.imageWarning
						.setImageResource(R.drawable.icon_warning_sensor_red);

			}
		} else
		{
			viewholder.frameOnOff.setVisibility(View.GONE);
			viewholder.imageWarning.setVisibility(View.GONE);
			viewholder.layoutTemp.setVisibility(View.VISIBLE);
			
			
			viewholder.imgTemp.setImageResource(R.drawable.temp_icon_small);
			viewholder.txtTemp.setVisibility(View.VISIBLE);
			viewholder.txtTemp.setText(item.getStatusValue() + "");

		}

		viewholder.nameDevice.setText(item.getDeviceName());

		viewholder.placeName.setText(item.getDevicePlace());

		onclick(viewholder, position);

		return convertView;
	}

	@SuppressWarnings("deprecation")
	public void onclick(final ViewHolder viewholder, final int position) {

		final DeviceItem ItemCurrent = mdeviceCardList.get(position);

		viewholder.slidingDrawer
				.setOnDrawerOpenListener(new OnDrawerOpenListener() {

					@Override
					public void onDrawerOpened() {
						/*
						 * viewholder.txtOn.setVisibility(View.INVISIBLE);
						 * viewholder.txtOff.setVisibility(View.INVISIBLE);
						 */
						viewholder.slidingDrawer
								.setBackgroundResource(R.drawable.icon_decive_on_small_source);
						mSQLite.updateDevice(ItemCurrent.getDeviceId(), 1, -1,
								-1, "-1", "-1", -1);
						

						((BaseActivity) mActivity).onRefreshData();
					}
				});

		viewholder.slidingDrawer
				.setOnDrawerCloseListener(new OnDrawerCloseListener() {

					@Override
					public void onDrawerClosed() {
						viewholder.slidingDrawer
								.setBackgroundResource(R.drawable.icon_device_off_small);
						mSQLite.updateDevice(ItemCurrent.getDeviceId(), 0, -1,
								-1, "-1", "-1", -1);
						/*
						 * viewholder.txtOff.setVisibility(View.INVISIBLE);
						 * viewholder.txtOn.setVisibility(View.INVISIBLE);
						 * viewholder
						 * .txtOn.setTextColor(mActivity.getResources()
						 * .getColor(R.color.color_device_off_text));
						 */
						((BaseActivity) mActivity).onRefreshData();
					}
				});

		viewholder.imageMenu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				mPopMenu = new PopupMenu(mActivity, viewholder.imageMenu);
				mPopMenu.getMenuInflater().inflate(
						R.menu.menu_list_device_card, mPopMenu.getMenu());
				mPopMenu.show();
				mPopMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

					@Override
					public boolean onMenuItemClick(MenuItem index) {
						// TODO Auto-generated method stub
						if (index.getItemId() == R.id.deviceMenu_editName) {

							/*
							 * ((HomeControlActivity) mActivity)
							 * .renameDeviceFuntion(ItemCurrent);
							 */
							Toast.makeText(mActivity, "Changed!", 1).show();
						}
						if (index.getItemId() == R.id.deviceMenu_editPlace) {
							Toast.makeText(mActivity, "Changed!", 1).show();
							/*
							 * ((HomeControlActivity) mActivity)
							 * .renameDeviceFuntion(ItemCurrent);
							 */

						}
						if (index.getItemId() == R.id.deviceMenu_editPhoto) {
							Toast.makeText(mActivity, "Edited!", 1).show();

							/*
							 * ((HomeControlActivity) mActivity)
							 * .renameDeviceFuntion(ItemCurrent);
							 */
						}
						if (index.getItemId() == R.id.deviceMenu_delete) {
							/*
							 * ((HomeControlActivity) mActivity)
							 * .deleteDeviceFuntion(ItemCurrent);
							 */
							Toast.makeText(mActivity, "Deleted!", 1).show();
						}
						return true;
					}
				});

			}
		});

		viewholder.nameDevice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intentDeviceCard = new Intent(mActivity,
						DeviceDetailActivity.class);
				Bundle bundleInfoDevice = new Bundle();

				bundleInfoDevice.putSerializable("item", ItemCurrent);

				intentDeviceCard.putExtras(bundleInfoDevice);
				mActivity.startActivity(intentDeviceCard);

			}
		});
		viewholder.placeName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intentDeviceCard = new Intent(mActivity,
						DeviceDetailActivity.class);
				Bundle bundleInfoDevice = new Bundle();

				bundleInfoDevice.putSerializable("item", ItemCurrent);

				intentDeviceCard.putExtras(bundleInfoDevice);
				mActivity.startActivity(intentDeviceCard);

			}
		});

	}

	static class ViewHolder {
		private FrameLayout frameOnOff;
		private TextView nameDevice;
		private TextView txtOn;

		private TextView txtOff;
		private TextView placeName;
		// private TextView placeNameDevice;
		private ImageView imageMenu;
		private SlidingDrawer slidingDrawer;
		private ImageButton handle;
		private ImageView imageWarning;
		private FrameLayout layoutTemp;
		private TextView txtTemp;
		private ImageView imgTemp;
		
	}

	public void swapItems(ArrayList<DeviceItem> mList) {
		this.mdeviceCardList = mList;
		notifyDataSetChanged();
	}

}
